<template>
    <div>
        <ly-dialog v-model="dialogVisible" :title="loadingTitle" width="760px" height="300px" :before-close="handleClose">
            <el-card class="box-card">

                <el-form :inline="false" :model="formData"  ref="rulesForm" label-position="right" label-width="auto">
                    <el-form-item label="脚本名称" prop="name">
                        <el-input v-model="formData.name"></el-input>
                    </el-form-item>
                    <el-form-item label="脚本语言" prop="language_type">
                        <!-- <el-input v-model="formData.language_type"></el-input> -->
                        <el-radio-group v-model="formData.language_type"  @change="changeLang" >
                            <el-radio-button value="python"   >python</el-radio-button>
                            <el-radio-button value="sh" >shell</el-radio-button>
                        </el-radio-group>
                    </el-form-item>
                    <!-- <el-form-item label="调用方式" prop="exec_method">
                        <el-input v-model="formData.exec_method"></el-input>
                    </el-form-item> -->
                    <el-form-item label="脚本参数" prop="exec_param">
                        <el-input v-model="formData.exec_param" placeholder="arg1 arg2"></el-input>
                    </el-form-item>
                    <el-form-item label="脚本内容" >

                    </el-form-item>

                    <aceEditor  @on-change-language="handleChangeLanguage"   v-model:value="formData.content" @update:value="formData.content = $event" ></aceEditor>
                    <!-- <testEditor  v-model:value="formData.content" @update:value="formData.content = $event" :language="language_type"></testEditor> -->
                </el-form>


            </el-card>
            <template #footer>
                    <el-button @click="handleClose" :loading="loadingSave">取消</el-button>
                    <el-button type="primary" @click="submitData" :loading="loadingSave">确定</el-button>
            </template>
        </ly-dialog>
    </div>
</template>

<script>
    import {ScriptAdd, ScriptEdit,} from "@/api/api";
    import LyDialog from "../../../../components/dialog/dialog";

    import aceEditor from '@/components/editor/aceEditor'
    import testEditor from '@/components/editor/testEditor'
    export default {
        name: "addModuleScript",
        components: {LyDialog,aceEditor,testEditor},
        emits: ['refreshData'],
        data() {
            return {
                dialogVisible:false,
                loadingSave:false,
                loadingTitle:'',
                lang: 'python',
                formData:{
                    name:'',
                    language_type: '',
                    exec_method: '',
                    content: '',
                    exec_param: '',
                    pre_exec_log: ''
                }
            }
        },
        methods:{
            handleClose() {
                this.dialogVisible=false
                this.loadingSave=false
                this.formData = {
                    name:'',
                    language_type: '',
                    exec_method: '',
                    content: '',
                    exec_param: '',
                    pre_exec_log: ''
                }
                this.lang = ''
                this.$emit('refreshData')
            },
            addUserFn(item,flag) {
                this.loadingTitle=flag
                this.dialogVisible=true
                if(item){
                    this.formData = item
                    if ( item.language_type === "shell" ){
                        this.lang = "sh"
                    } else {
                        this.lang = "python"
                    }
                    this.handleChangeLanguage()
                }
            },
            changeLang(value) {
                this.lang = value
                this.handleChangeLanguage()
            },
            handleChangeLanguage(changeLanguageFunc) {
                // console.log(this.lang)
                // 确保 changeLanguageFunc 不是 undefined
                if (typeof changeLanguageFunc === 'function') {
                    // 调用从事件中传递过来的方法
                    changeLanguageFunc(this.lang); // 示例：切换到 Python 模式
                }
            },
            submitData() {
                if(this.formData.name === ""){
                    this.loadingSave=false
                    this.$message.warning("脚本名称不能为空")
                    return
                }
                if (this.formData.id) {
                    ScriptEdit(this.formData).then(res => {
                        if (res.code === 2000) {
                            this.$message.success(res.msg)
                            this.handleClose()
                        } else {
                            this.$message.warning(res.msg)
                        }
                    })
                } else {
                    ScriptAdd(this.formData).then(res => {
                        if (res.code === 2000) {
                            this.$message.success(res.msg)
                            this.handleClose()
                        } else {
                            this.$message.warning(res.msg)
                        }
                    })
                }
            },
        }
    }
</script>